import { getRecordListApi } from '@/api/system/record/record'
import { RecordListParam } from '@/api/system/record/RecordModel'
import { StatusCode } from '@/http/request'
import formatDate from '@/utils/filters'
import { nextTick, onMounted, reactive, ref } from 'vue'

export default function useRecordTable() {
    // 表格高度
    const tableHeight = ref(0)

    // 定义表格数据
    const recordTable = reactive({
        list: []
    })

    // 定义列表查询的参数
    const listParam = reactive<RecordListParam>({
        current_page: 1,
        page_size: 20,
        method: '',
        path: '',
        status: 200,
        total: 0
    })

    // 获取操作记录列表
    const getRecordList = async () => {
        let res = await getRecordListApi(listParam)
        if (res && res.code == StatusCode.Success) {
            console.log('操作记录列表：', res)
            recordTable.list = res.data.list
            recordTable.list.forEach((item: any) => {
                item.created_at = formatDate(item.created_at)
            })
            listParam.total = res.data.total
        }
    }
    // 页容量改变时触发
    const sizeChange = (size: number) => {
        listParam.page_size = size
        getRecordList()
    }

    // 页数改变时触发
    const currentChange = (page: number) => {
        listParam.current_page = page
        getRecordList()
    }

    // 搜索
    const searchBtn = () => {
        getRecordList()
    }
    // 重置
    const resetBtn = () => {
        listParam.method = ''
        listParam.path = ''
        listParam.status = 200
        listParam.current_page = 1
        getRecordList()
    }

    // 首次加载
    onMounted(() => {
        getRecordList()
        nextTick(() => {
            tableHeight.value = window.innerHeight - 210
        })
    })

    return {
        tableHeight,
        recordTable,
        listParam,
        getRecordList,
        sizeChange,
        currentChange,
        searchBtn,
        resetBtn
    }
}
